package com.sgb.kjwl.view.ui.activity.transport.driver.trans;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.sgb.capital.model.QRcodeEntity;
import com.sgb.capital.view.ui.activity.QrCodeActivity;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTransportFreightCollectionBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.view.ui.activity.transport.OfflineCollectionConfirmActivity;
import com.swgk.core.dialog.RoundDialog;
import com.sgb.kjwl.viewmodel.transport.TransportCollectionModel;
import com.swgk.core.base.di.AppComponent;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

/**
 * Time:2021/3/9
 * Author:黄淋
 * Description:运费收款
 */
public class TransportFreightCollectionActivity extends AppActivity {
    private ActivityTransportFreightCollectionBinding mBinding;
    public static final int OFFLINE_REQUEST_CODE = 200;
    public static final int OFFLINE_RESULT_CODE = 201;
    String freight, orderNo;
    @Inject
    TransportCollectionModel viewModel;
    Map mMap;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_transport_freight_collection);
        mBinding.actionBar.setOnBackClickListener(view -> finish());
        mBinding.ivStateErweima.setSelected(false);//2.1.4隐藏了
        mBinding.ivStateKuaijiezhifu.setSelected(true);
        mBinding.ivStateOffline.setSelected(false);
        mBinding.rlErweima.setOnClickListener(this);
        mBinding.rlKuaijiezhifu.setOnClickListener(this);
        mBinding.rlOffline.setOnClickListener(this);
        mBinding.tvConfirm.setOnClickListener(this);
        freight = getIntent().getStringExtra("infoPrice");
        orderNo = getIntent().getStringExtra("orderNo");
        mBinding.tvFreight.setText(freight == null || freight.isEmpty() ? "" : freight + "元");
        mMap = new HashMap();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_erweima:
                mBinding.ivStateErweima.setSelected(false);
                mBinding.ivStateKuaijiezhifu.setSelected(false);
                mBinding.ivStateOffline.setSelected(false);
                break;
            case R.id.rl_kuaijiezhifu:
                mBinding.ivStateErweima.setSelected(false);
                mBinding.ivStateKuaijiezhifu.setSelected(true);
                mBinding.ivStateOffline.setSelected(false);
                break;
            case R.id.rl_offline:
                mBinding.ivStateErweima.setSelected(false);
                mBinding.ivStateKuaijiezhifu.setSelected(false);
                mBinding.ivStateOffline.setSelected(true);
                break;
            case R.id.tv_confirm:
                if (mBinding.ivStateErweima.isSelected()) {//二维码支付
                    viewModel.getPayFreight(TransportFreightCollectionActivity.this, orderNo);
                    viewModel.ldTradeNo.observe(TransportFreightCollectionActivity.this, info -> {
                        if ("".equals(info.getFreightOrderNo())) {
                            return;
                        }
                        QRcodeEntity qRcodeEntity = new QRcodeEntity();
                        qRcodeEntity.amount = info.getAmount() + "";
                        qRcodeEntity.orderId = info.getFreightOrderNo();
                        qRcodeEntity.payType = "WX";
                        String json = new Gson().toJson(qRcodeEntity);
                        QrCodeActivity.start(getActivity(), json);
                    });
                } else if (mBinding.ivStateKuaijiezhifu.isSelected()) {//线上支付
                    new RoundDialog.Builder(this)
                            .title("提示")
                            .content("确定线上收款后，请尽快联系货主支付运费。是否继续操作“线上收款”？")
                            .cancel("取消")
                            .confirm("确定")
                            .cancelable(false)
                            .confirmCallback(v -> {
                                viewModel.onLinePayMethod(this, orderNo);
                            })
                            .show();

                } else {//线下收款
                    if (!TextUtils.isEmpty(orderNo) && !TextUtils.isEmpty(freight))
                        OfflineCollectionConfirmActivity.startForResult(this, orderNo, freight, OFFLINE_REQUEST_CODE);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OFFLINE_REQUEST_CODE && resultCode == OFFLINE_RESULT_CODE) {
            finish();
        }
    }
}
